﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using KPIS.GERP.Models;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.SYS.ViewModel;
using KPIS.GERP.WEB.GIMS.Filters;
using WebMatrix.WebData;

namespace KPIS.GERP.WEB.GIMS
{
	public static class GerpLog
	{
		private static string GetPreviousMethodName()
		{
			StackTrace stackTrace = new StackTrace();
			MethodBase methodBase2 = stackTrace.GetFrame(2).GetMethod();
			MethodBase methodBase1 = stackTrace.GetFrame(1).GetMethod();
			MethodBase methodBase0 = stackTrace.GetFrame(0).GetMethod();
			return string.Format("{2}/{1}/{0}", methodBase2, methodBase1, methodBase0);
		}

		public static void Log(string message, int code = 0)
		{
			Log(new LogException(GetPreviousMethodName() + ": " + message, code));
		}

		public static void Warning(string message, Exception innerException = null)
		{
			Log(new WarningException(message, innerException));
		}

		public static void Error(string message, Exception innerException = null)
		{
			Log(new ErrorException(message, innerException));
		}

		public static void Unknown(string message, Exception innerException = null)
		{
			Log(new UnknownException(message, innerException));
		}
		public static void DatabaseException(string message, Exception innerException = null)
		{
			Log(new DataBaseException(message, innerException));
		}

		public static void ApplicationException(string message, Exception innerException = null)
		{
			Log(new ApplicationException(message, innerException));
		}

		public static void NullReferenceException(string message, Exception innerException = null)
		{
			Log(new NullReferenceException(message, innerException));
		}

		public static void ArgumentNullException(string paramName, string message)
		{
			Log(new ArgumentNullException(paramName, message));
		}

		public static void InvalidOperationException(string message, Exception innerException = null)
		{
			Log(new InvalidOperationException(message, innerException));
		}

		public static void Log(Exception e)
		{ 
			Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(e, HttpContext.Current));
		}
	}

	[Serializable]
	public class LogException : Exception
	{
		public LogException(string message, int code)
			: base(message)
		{ 
		}

		public LogException(string message, Exception innerException)
			: base(message, innerException)
		{
		}
		public LogException(string format, Exception innerException, params object[] args)
			: base(string.Format(format, args), innerException)
		{
		} 
	}

	[Serializable]
	public class WarningException : Exception
	{
		public WarningException(string message, int code)
			: base(message)
		{
		}

		public WarningException(string message, Exception innerException)
			: base(message, innerException)
		{
		}
		public WarningException(string format, Exception innerException, params object[] args)
			: base(string.Format(format, args), innerException)
		{
		}
	}
	[Serializable]
	public class ErrorException : Exception
	{
		public ErrorException(string message, int code)
			: base(message)
		{
		}

		public ErrorException(string message, Exception innerException)
			: base(message, innerException)
		{
		}
		public ErrorException(string format, Exception innerException, params object[] args)
			: base(string.Format(format, args), innerException)
		{
		}
	}
	[Serializable]
	public class UnknownException : Exception
	{
		public UnknownException(string message, int code)
			: base(message)
		{
		}

		public UnknownException(string message, Exception innerException)
			: base(message, innerException)
		{
		}
		public UnknownException(string format, Exception innerException, params object[] args)
			: base(string.Format(format, args), innerException)
		{
		}
	}
	[Serializable]
	public class DataBaseException : Exception
	{
		public DataBaseException(string message, int code)
			: base(message)
		{
		}

		public DataBaseException(string message, Exception innerException)
			: base(message, innerException)
		{
		}
		public DataBaseException(string format, Exception innerException, params object[] args)
			: base(string.Format(format, args), innerException)
		{
		}
	}
}